# Copyright 2018- The Pixie Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0

load("@io_bazel_rules_docker//container:container.bzl", "container_layer")
load("//bazel:linux_headers.bzl", "gen_timeconst")
load("//bazel:pl_build_system.bzl", "pl_cc_library")

package(default_visibility = [
    "//src/stirling:__subpackages__",
])

pl_cc_library(
    name = "cc_library",
    srcs = glob(
        ["*.cc"],
        exclude = [
            "**/*_test.cc",
        ],
    ),
    hdrs = glob(
        ["*.h"],
        exclude = [
            "**/*_mock.h",
        ],
    ),
    visibility = [
        "//src/e2e_test/vizier/planner/dump_schemas:__pkg__",
        "//src/experimental/standalone_pem:__subpackages__",
        "//src/stirling:__subpackages__",
        "//src/vizier/services/agent:__subpackages__",
    ],
    deps = [
        "//src/shared/types/typespb/wrapper:cc_library",
        "//src/stirling/bpf_tools:cc_library",
        "//src/stirling/core:cc_library",
        "//src/stirling/proto:stirling_pl_cc_proto",
        "//src/stirling/source_connectors/dynamic_bpftrace:cc_library",
        "//src/stirling/source_connectors/dynamic_tracer:cc_library",
        "//src/stirling/source_connectors/jvm_stats:cc_library",
        "//src/stirling/source_connectors/network_stats:cc_library",
        "//src/stirling/source_connectors/perf_profiler:cc_library",
        "//src/stirling/source_connectors/pid_runtime:cc_library",
        "//src/stirling/source_connectors/pid_runtime_bpftrace:cc_library",
        "//src/stirling/source_connectors/proc_exit:cc_library",
        "//src/stirling/source_connectors/proc_stat:cc_library",
        "//src/stirling/source_connectors/process_stats:cc_library",
        "//src/stirling/source_connectors/seq_gen:cc_library",
        "//src/stirling/source_connectors/socket_tracer:cc_library",
        "//src/stirling/source_connectors/stirling_error:cc_library",
        "//src/stirling/source_connectors/tcp_stats:cc_library",
        "//src/stirling/utils:cc_library",
    ],
)

# Used by stirling runtime to install linux headers on hosts having no Linux headers pre-installed.
#
# Note that each linux header tarball is ~15MB compressed and ~230MB uncompressed.
# Trimmed versions are ~8-11MB (8.6 MB avg) compressed and ~45MB uncompressed.
# These are all trimmed versions, so we have 9MB * 29 = ~250 MB of headers.
stirling_linux_headers_tarballs = select({
    "@platforms//cpu:aarch64": ["@linux_headers_merged_arm64_tar_gz//file:file"],
    "@platforms//cpu:x86_64": ["@linux_headers_merged_x86_64_tar_gz//file:file"],
})

# Used by stirling runtime to replace the on-the-fly installed Linux headers from
# stirling_linux_headers_tarballs, in order to match the host's actual configs.
#
# See GenTimeConst() in src/stirling/utils/linux_headers.h for more details.
gen_timeconst(name = "stirling_linux_timeconst_files")

# Used by stirling runtime to provide symbolization for Java application profiling.
stirling_java_profiling_tools = [
    "//src/stirling/source_connectors/perf_profiler/java/px_jattach:px_jattach",
    "//src/stirling/source_connectors/perf_profiler/java/agent:agent",
]

# Use this binary to dynamically turn on/off Stirling runtime's debug logging.
# See src/stirling/e2e_tests/stirling_signal_test.sh for its usage.
stirling_ctrl = [
    "//src/stirling/binaries:stirling_ctrl",
]

stirling_collateral = [":stirling_linux_timeconst_files"] + stirling_ctrl + stirling_java_profiling_tools

container_layer(
    name = "bpf_layer",
    directory = "/px",
    files = stirling_collateral,
    tars = stirling_linux_headers_tarballs,
    visibility = ["//visibility:public"],
)
